package com.wx.annotation.inf.imp;

import com.sun.deploy.net.HttpRequest;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ParameterList;
import com.wx.annotation.Delete;
import com.wx.annotation.Insert;
import com.wx.annotation.Select;
import com.wx.annotation.Update;
import com.wx.annotation.inf.UserMapper;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-06 9:55
 */
public class MethodProxyFactory implements UserMapper {

    public static UserMapper getBean(Class<UserMapper> clazz){
        return new MethodProxyFactory();
    }


    @Override
    public void addUser(String id, String name, String sex, Integer age, Boolean isDelete)
    {
        Method[] methods = UserMapper.class.getDeclaredMethods();//getDeclaredMethods是获取所有属性对象
        for (Method method:methods) {
            if(method.isAnnotationPresent(Insert.class)){//isAnnotationPresent：判断method是否有insert注解
                String sql = method.getAnnotation(Insert.class).value();//getAnnotation获取类型为insert的注解
                //做其他操作
                sql = StringUtils.replaceOnce(sql, "?", id);
                sql = StringUtils.replaceOnce(sql, "?", name);
                sql = StringUtils.replaceOnce(sql, "?", sex);
                sql = StringUtils.replaceOnce(sql, "?", age.toString());
                sql = StringUtils.replaceOnce(sql, "?", isDelete.toString());
                System.out.println(sql);
            }
        }
    }

    @Override
    public void updateUser(String id, String name, String sex, Integer age, Boolean isDelete) {

        Method[] methods = UserMapper.class.getDeclaredMethods();
        for (Method method:methods) {
            if(method.isAnnotationPresent(Update.class)){
                String sql = method.getAnnotation(Update.class).value();
                System.out.println(sql);
                //做其他操作

            }
        }
    }

    @Override
    public void delete(String id) {

        Method[] methods = UserMapper.class.getDeclaredMethods();
        for (Method method:methods) {
            if(method.isAnnotationPresent(Delete.class)){
                String sql = method.getAnnotation(Delete.class).value();
                System.out.println(sql);
                //做其他操作

            }
        }
    }

    @Override
    public List selectAll() {

        Method[] methods = UserMapper.class.getDeclaredMethods();
        for (Method method:methods) {
            if(method.isAnnotationPresent(Select.class)){
                String sql = method.getAnnotation(Select.class).value();
                System.out.println(sql);
                //做其他操作

            }
        }
        return null;
    }
}
